package cn.hhchat.hutool_re.io;

/**
 * Created this one by HMH on 2017/12/27.
 */
public class FastByteBuffer {

    private byte[][] buffers = new byte[16][];

    private int buffersCount;

    private int currentBufferIndex = -1;

    private byte[] currentBuffer;

    private int offset;

    private int size;

    private final int minChunkLen;


    public FastByteBuffer() {
        this.minChunkLen = 1024;
    }

    public FastByteBuffer(int size) {
        this.minChunkLen = Math.abs(size);
    }

    private void needNewBuffer(int newSize) {
        int delta = newSize - size;
        int newBufferSize = Math.max(minChunkLen, delta);

        currentBufferIndex++;
        currentBuffer = new byte[newBufferSize];
        offset = 0;

        //扩容
        if (currentBufferIndex >= buffers.length) {
            int newLen = buffers.length << 1;
            byte[][] newBuffers = new byte[newLen][];
            System.arraycopy(buffers, 0, newBuffers, 0, buffers.length);
            buffers = newBuffers;
        }
        //
        buffers[currentBufferIndex] = currentBuffer;
        buffersCount++;
    }

    public FastByteBuffer append(byte[] array, int off, int len) {
        int end = off + len;
        if ((off < 0) || (len < 0) || (end > array.length)) {
            throw new IndexOutOfBoundsException();
        }
        //添加后size
        int newSize = size + len;
        //还剩多少没有添加
        int remaining = len;
        if (currentBuffer != null) {
            //拆分
            int part = Math.min(remaining, currentBuffer.length);
            System.arraycopy(array, end - remaining, currentBuffer, offset, part);
            remaining -= part;
            offset += part;
            size += part;
        }
        if (remaining > 0) {
            needNewBuffer(newSize);
            int part = Math.min(remaining, currentBuffer.length);
            System.arraycopy(array, end - remaining, currentBuffer, offset, part);
            offset += part;
            size += part;
        }
        return this;
    }

    public FastByteBuffer append(byte[] array) {
        return append(array, 0, array.length);
    }

    public FastByteBuffer append(byte element) {
        if ((currentBuffer == null) || offset == currentBuffer.length) {
            needNewBuffer(size + 1);
        }
        currentBuffer[offset] = element;
        offset++;
        size++;
        return this;
    }

    public FastByteBuffer append(FastByteBuffer buff) {
        if (buff.size == 0) {
            return this;
        }
        for( int i = 0 ; i < buff.currentBufferIndex; i++) {
            append(buff.buffers[i]);
        }
        append(buff.currentBuffer, 0, buff.offset);
        return this;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 当前缓冲位于缓冲区的索引位
     *
     * @return {@link #currentBufferIndex}
     */
    public int index() {
        return currentBufferIndex;
    }

    public int offset() {
        return offset;
    }

    public byte[] array(int index) {
        return buffers[index];
    }

    public void reset() {
        size = 0;
        offset = 0;
        currentBufferIndex = -1;
        currentBuffer = null;
        buffersCount = 0;
    }

    public byte[] toArray() {
        int pos = 0;
        byte[] array = new byte[size];
        if (currentBufferIndex == -1) {
            return array;
        }
        for( int  i = 0; i < currentBufferIndex; i++) {
            int len = buffers[i].length;
            System.arraycopy(buffers[i], 0, array, pos, len);
            pos += len;
        }
        System.arraycopy(buffers[currentBufferIndex], 0, array, pos, offset);
        return array;
    }

    public byte[] toArray(int start, int len) {
        int remain = len;
        int pos = 0;
        byte[] array = new byte[len];

        if (len == 0) {
            return array;
        }

        int i = 0 ;
        while (start >= buffers[i].length) {
            start -= buffers[i].length;
            i++;
        }

        while (i < buffersCount) {
            byte[] buf = buffers[i];
            int c = Math.min(buf.length - start, remain);
            System.arraycopy(buf, start, array, pos, c);
            pos += c;
            remain -= c;
            if (remain == 0) {
                break;
            }
            start = 0;
            i ++;
        }
        return array;
    }

    public byte get(int index) {
        if ((index >= size) || (index < 0)) {
            throw new IndexOutOfBoundsException();
        }
        int i = 0;
        while (index >= buffers[i].length) {
            index -= buffers[i].length;
            i++;
        }
        return buffers[i][index];
    }
}
